package com.xyh.mapper;

import com.xyh.entity.YxcUser;
import com.xyh.entity.YxcUsersYhq;
import com.xyh.entity.YxcYhq;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface YxcUsersYhqMapper {
    @Select("select distinct * from yxc_users_yhq where owner_telephone = #{owner_telephone}")
    List<YxcUsersYhq> getAllYhqByPhone(@Param("owner_telephone") String owner_telephone);

    @Update("UPDATE yxc_users_yhq set cy_num = (SELECT cy_num FROM (SELECT cy_num FROM yxc_users_yhq \n" +
            "   WHERE owner_telephone = #{owner_telephone} and yhq_bh = #{yhq_bh}) AS tmp) - 1 \n" +
            "\t WHERE owner_telephone = #{owner_telephone} and yhq_bh = #{yhq_bh}")
    int updateNumByPhoneAndBh(@Param("owner_telephone") String owner_telephone,@Param("yhq_bh") int yhq_bh);

    @Delete("DELETE FROM yxc_users_yhq where owner_telephone = #{owner_telephone} and yhq_bh = #{yhq_bh} and cy_num = 0")
    int deleteByPhoneAndBhAndNum(@Param("owner_telephone") String owner_telephone,@Param("yhq_bh") int yhq_bh);

    @Update("UPDATE yxc_users_yhq set cy_num = ( \n" +
            "\t SELECT cy_num FROM ( \n" +
            "\t\t SELECT cy_num FROM yxc_users_yhq where owner_telephone = #{owner_telephone} and yhq_bh = #{yhq_bh} ) as tmp ) + #{num_add}  \n" +
            "\t\t where owner_telephone = #{owner_telephone} and yhq_bh = #{yhq_bh}")
    int updateCyNumByPhoneAndBh(@Param("owner_telephone") String telephone,@Param("yhq_bh") int yhq_bh,@Param("num_add") int num_add);

    @Insert("INSERT INTO yxc_users_yhq(owner_name,owner_telephone,yhq_bh,yhq_name,cy_num) VALUES(#{user_name},#{telephone},#{yhq_bh},#{yhq_name},#{cy_num})")
    int addOneUserYhq(@Param("user_name") String user_name,@Param("telephone") String telephone,@Param("yhq_bh") int yhq_bh,@Param("yhq_name") String yhq_name,@Param("cy_num") int cy_num);
}
